﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace BMWKT
{
	public partial class GameConfigSaved : Form
	{
		private string backupZipPath;

		private string decompressionPath;

		public GameConfigSaved()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			string filePath = GetBMWKInstallPathForProcess.GameConfigSavedPath;
			BackupFileToZip backupFileToZip = new BackupFileToZip(label1, checkBox1, textBox1, checkedListBox1, filePath);
			backupFileToZip.CreateGameConfigBackupFile();
		}

		private void button2_Click(object sender, EventArgs e)
		{
			textBox1.Enabled = false;
			new ReadGameSavedBackupInfo();
			List<string> backupFileNames = ReadGameSavedBackupInfo.GetGameSavedBackupFileName("玩家配置");

			checkedListBox1.Items.Clear();

			foreach (var fileName in backupFileNames)
			{
				checkedListBox1.Items.Add(fileName);
			}
		}

		private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
		{
			// 如果选择的是"Checked"状态，那么取消其他项的选中
			if (e.NewValue == CheckState.Checked)
			{
				for (int i = 0; i < checkedListBox1.Items.Count; i++)
				{
					// 如果选中状态是"Unchecked"并且不是当前正在检查的项，则取消选中
					if (i != e.Index)
					{
						checkedListBox1.SetItemChecked(i, false);
					}
				}
			}
			button3.Enabled = true;
			string fileName = checkedListBox1.Text;
			backupZipPath = ReadGameSavedBackupInfo.GetGameSavedBackupFilePath(fileName);
			decompressionPath = ReadGameSavedBackupInfo.GetGameSavedOriginalFilePath(fileName);
			textBox1.Text = decompressionPath;
			string message = $"游戏配置备份: {fileName}\n\n文件大小: {ReadGameSavedBackupInfo.GetGameSavedBackupFileSize(fileName)}\n\n备份时间: {ReadGameSavedBackupInfo.GetGameSavedBackupFileTime(fileName)}\n\n备份路径: {backupZipPath}";
			label1.Text = message;
		}

		private void button3_Click(object sender, EventArgs e)
		{
			new UnzipFile(backupZipPath, decompressionPath);
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if (checkBox1.Checked)
			{
				textBox1.Enabled = true;
			}
			else 
			{
				textBox1.Enabled = false;
			}
		}
	}
}
